package com.ailesson.video.controller;


import com.ailesson.common.except.GlobalErrorCodeConstants;
import com.ailesson.common.reply.R;
import com.ailesson.video.util.FileUploadUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "文件模块")
@RestController
@RequestMapping("/admin/file")
public class FileController {

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @Operation(summary = "上传单个文件")
    @Parameter(name = "file",in = ParameterIn.HEADER, description = "文件对象")
    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file){
        try {
            return R.success(fileUploadUtil.upload(file));
        } catch (Exception e) {
            return R.error(GlobalErrorCodeConstants.FILE_UPLOAD_ERROR);
        }
    }

    @Operation(summary = "上传视频")
    @Parameters(value = {
            @Parameter(name = "file",in = ParameterIn.HEADER, description = "视频对象"),
            @Parameter(name = "chapterName",in = ParameterIn.QUERY, description = "章节名称")
    })
    @PostMapping("/video/upload")
    public R videoUpload(@RequestParam("file") MultipartFile file,@RequestParam("chapterName") String chapterName){
        try {
            return R.success(fileUploadUtil.videoUpload(file,chapterName));
        } catch (Throwable e) {
            return R.error(GlobalErrorCodeConstants.FILE_DOWNLOAD_ERROR);
        }
    }
}
